package cn.iocoder.yudao.module.pickbook.service.user;

import cn.iocoder.yudao.module.pickbook.controller.admin.user.vo.PickUserPageReqVO;
import cn.iocoder.yudao.module.pickbook.controller.admin.user.vo.PickUserSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.pickbook.dal.dataobject.user.PickUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.user.PickUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 用户 Service 实现类
 *
 * @author dingli
 */
@Service
@Validated
public class PickUserServiceImpl implements PickUserService {

    @Resource
    private PickUserMapper pickUserMapper;

    @Override
    public Long createUser(PickUserSaveReqVO createReqVO) {
        // 插入
        PickUserDO user = BeanUtils.toBean(createReqVO, PickUserDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        user.setId(id);

        pickUserMapper.insert(user);

        // 返回
        return user.getId();
    }

    @Override
    public void updateUser(PickUserSaveReqVO updateReqVO) {
        // 校验存在
        validateUserExists(updateReqVO.getId());
        // 更新
        PickUserDO updateObj = BeanUtils.toBean(updateReqVO, PickUserDO.class);
        pickUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteUser(Long id) {
        // 校验存在
        validateUserExists(id);
        // 删除
        pickUserMapper.deleteById(id);
    }

    @Override
        public void deleteUserListByIds(List<Long> ids) {
        // 删除
        pickUserMapper.deleteByIds(ids);
        }


    private void validateUserExists(Long id) {
        if (pickUserMapper.selectById(id) == null) {
            throw exception(USER_NOT_EXISTS);
        }
    }

    @Override
    public PickUserDO getUser(Long id) {
        return pickUserMapper.selectById(id);
    }

    @Override
    public PageResult<PickUserDO> getUserPage(PickUserPageReqVO pageReqVO) {
        return pickUserMapper.selectPage(pageReqVO);
    }
}